﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CMSSetting.aspx.cs" Inherits="WinnerCMS.WebUI.Admin.ControlPanel.CMSSetting" EnableViewState="False" %>

<%@ Import Namespace="System.ComponentModel" %>
<%@ Import Namespace="WinnerCMS.BLL" %>
<%@ Import Namespace="WinnerCMS.Common" %>
<%@ Import Namespace="WinnerCMS.Model" %>
 
<!doctype html>
<html>
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <meta charset="utf-8" />
    <meta http-equiv="Pragma" content="No-cach" />
    <%: Scripts.Render("~/Scripts/AdminBase") %>
    <%: Styles.Render("~/Content/Bootstrap/css") %>
 
    <script runat="server">

        private readonly WinnerCMS.BLL.Config.UI ui = new WinnerCMS.BLL.Config.UI();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["Action"] != null)
            {
                var res = Save(Request["typeName"]);
                Response.Write(res.ToJson());
                Response.End();
                return;
            }

            var config = CMSConfig.Instance();

            //Response.Write(config.ToJSON());
            for (var index = 0; index < this.ui.GetTabs().Count; index++)
            {
                var v = this.ui.GetTabs()[index];

                var t = new Table
                {
                    CssClass = "TabelGrid",
                    ID = "Tab" + index,
                    Width = new System.Web.UI.WebControls.Unit("100%"),
                    EnableViewState = false
                };
                t.Attributes["data-typeName"] = v.Value;

                object shili;
                var property = config.GetType().GetProperty(v.Value);
                if (property == null)
                {
                    shili = config;
                }
                else
                {
                    shili = property.GetValue(config, null);
                }

                foreach (var p in this.ui.GetValueTypeProperties(v.Value))
                {
                    var category = p.GetCustomAttributes(typeof (CategoryAttribute), true).FirstOrDefault();
                    if (category != null) continue;
                    var display = p.GetCustomAttributes(typeof (DisplayNameAttribute), true).FirstOrDefault() as DisplayNameAttribute;

                    var displayName = display == null ? p.Name : display.DisplayName;

                    var row = new TableRow();
                    var leftCell = new TableCell
                    {
                        CssClass = "RowTitle",
                        Text = displayName + "："
                    };
                    var rightCell = new TableCell();
                    rightCell.Controls.Add(ControlBuilder(v.Value, p.Name, p.GetValue(shili, null)));

                    row.Cells.Add(leftCell);
                    row.Cells.Add(rightCell);
                    t.Rows.Add(row);
                }
                var divs = new HtmlGenericControl
                {
                    TagName = "div"
                };
                divs.Attributes["title"] = v.Key;
                divs.Controls.Add(t);
                PlaceHolder1.Controls.Add(divs);
            }
        }

        private Result Save(string typeFullName)
        {
            var res = new Result();

            //Type cfg = ConfigManage.SystemConfigs(typeFullName);
            //var config = SystemConfigManage.Read(cfg);
            //foreach (var property in cfg.GetProperties())
            //{
            //    string value = Request.Form[typeFullName + "_" + property.Name];

            //    object ojb = SystemConfigManage.SetValue(cfg, property.Name, value);
            //    if (ojb == null)
            //    {
            //        continue;
            //    }
            //    property.SetValue(config, ojb, null);
            //}

            //SystemConfigManage.Save(config);


            return res;
        }


        protected WebControl ControlBuilder(string type, string name, object value)
        {
            WebControl control;

            if (value is string || value is int || value is double)
            {
                control = new System.Web.UI.WebControls.TextBox
                {
                    Text = value.ToString(),
                    Width = new System.Web.UI.WebControls.Unit("260px")
                };
            }
            else if (value is bool)
            {
                control = new WinnerCMS.WebControls.BootStrap.CheckBox {Checked = (bool) value};
                ((WinnerCMS.WebControls.BootStrap.CheckBox) control).InputAttributes["value"] = "true";
            }
            else if (value is string[] || value is List<string>)
            {
                control = new System.Web.UI.WebControls.TextBox
                {
                    Text = string.Join("\r\n", (IEnumerable<string>) value),
                    TextMode = TextBoxMode.MultiLine,
                    Columns = 80,
                    Rows = 6
                };
            }
            else
            {
                control = new Label {Text = value.ToString()};
            }
            control.ID = type + "_" + name;

            return control;
        }

    </script>
    <script>
        function checkChange(obj) {
            if (true == $(obj).prop("checked")) {
                obj.value = "true";
            } else {
                obj.value = "false";
            }
        }

        function submitForm(fullName) {
            $.ajax({
                cache: true,
                type: "POST",
                url: "/api/Config/Save",
                data: {
                    "": $('#form1').serialize()
                },
                async: false,
                error: function (request) {
                    alert("Connection error");
                },
                success: function (data) {
                    alert("保存成功！");
                }
            });
            return false;
        }
        function PostBackData() {
            $(".TabelGrid");
        }
    </script>
    <style>
        #tabs {
            width: 100%;
        }

        .ui-tabs-vertical {
            width: 55em;
        }

            .ui-tabs-vertical .ui-tabs-nav {
                padding: .2em .1em .2em .2em;
                float: left;
                width: 12em;
            }

                .ui-tabs-vertical .ui-tabs-nav li {
                    clear: left;
                    width: 100%;
                    border-bottom-width: 1px !important;
                    border-right-width: 0 !important;
                    margin: 0 -1px .2em 0;
                }

                    .ui-tabs-vertical .ui-tabs-nav li a {
                        display: block;
                    }

                    .ui-tabs-vertical .ui-tabs-nav li.ui-tabs-active {
                        padding-bottom: 0;
                        padding-right: .1em;
                        border-right-width: 1px;
                        border-right-width: 1px;
                    }

            .ui-tabs-vertical .ui-tabs-panel {
                padding: 1em;
                float: right;
                width: 40em;
            }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div id="tabs" class="easyui-tabs">
            
            <asp:PlaceHolder ID="PlaceHolder1" runat="server" EnableViewState="False"></asp:PlaceHolder>
        </div>
        <div style="width: 120px; text-align: center; margin-left: auto; margin-right: auto;">
            <BootStrap:Button ID="btnOK" runat="server" Text="保存" OnClientClick="return submitForm();" EnableViewState="False" />
        </div>
    </form>
</body>
</html>
